﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using teacher.Util;
using System.Data;
using System.Collections;

namespace tearcher.Controllers
{
    public class SystemController : Controller
    {
        /// <summary>
        /// 获取关于我们
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AppAuthorizeAttribute(Users = "getAboutUS")]
        public String getAboutUS()
        {
            try
            {
                DataTable dt = DbHelper.ExecuteDataTable("select * from T_AboutUS");
                if (dt.Rows.Count > 0)
                {
                    return Helper.sendOKJson(Request, new { data = dt.Rows[0]["aboutUS"] });
                }
                else
                {
                    return Helper.sendErrorJson(Request, "获取关于我们失败！");
                }
            }
            catch (Exception e)
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误");
            }
        }

        /// <summary>
        /// 获取版本
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AppAuthorizeAttribute(Users = "getVersions")]
        public String getVersions()
        {
            try
            {
                DataTable dt = DbHelper.ExecuteDataTable("select top 1 versionName,versionNo,androidAppURL from T_Versions where type=0 order by creattime desc");
                if (dt.Rows.Count > 0)
                {
                    return Helper.sendOKJson(Request, new { versionName = dt.Rows[0]["versionName"], versionNo = dt.Rows[0]["versionNo"], androidAppURL = dt.Rows[0]["androidAppURL"] });
                }
                else
                {
                    return Helper.sendErrorJson(Request, "获取版本失败！");
                }
            }
            catch (Exception e)
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误");
            }
        }

        /// <summary>
        /// 反馈
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AppAuthorizeAttribute(Users = "addFeedback")]
        public String addFeedback(string suggest)
        {
            try
            {
                DataTable dt_token = Helper.getToken(Request);
                DbHelper.ExecuteNonQuery(string.Format("insert into T_Feedback(userId,suggest,creatTime) values('{0}','{1}',getDate())",dt_token.Rows[0]["id"],suggest));
                return Helper.sendOKJson(Request, null);

            }
            catch (Exception e)
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误");
            }
        }
        /// <summary>
        /// 字典 -- 列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AppAuthorizeAttribute(Users = "getDictionaryList")]
        public String getDictionaryList(string type, string keyword)
        {
            try
            {
                string condition = "";
                if (!string.IsNullOrEmpty(keyword)) {
                    condition += " and (name like '%" + keyword + "%' or id in(select pid from T_Dictionary where name like '%"+keyword+"%'))";
                }
                if (!string.IsNullOrEmpty(type))
                {
                    condition += " and type='" + type + "'";
                }
                DataTable dt = DbHelper.ExecuteDataTable("T_Dictionary", "*", "id", condition, "id asc", 1, 100);
                List<Hashtable> list = new List<Hashtable>();
                for (int i = 0; i < dt.Rows.Count; i++) {
                    if (string.IsNullOrEmpty(dt.Rows[i]["pid"].ToString()))
                    {
                        Hashtable ht = new Hashtable();
                        ht["text"]=dt.Rows[i]["name"];
                        ht["id"]=dt.Rows[i]["id"];
                        list.Add(ht);
                    }
                }

                for (int i = 0; i < list.Count; i++) {
                    List<Hashtable> itemList = new List<Hashtable>();
                    for (int j = 0; j < dt.Rows.Count; j++) {
                        if (list[i]["id"].ToString().Equals(dt.Rows[j]["pid"].ToString())) {
                            Hashtable ht = new Hashtable();
                            ht["text"] = dt.Rows[j]["name"];
                            ht["id"] = dt.Rows[j]["id"];
                            ht["class0"] = "ion-ios-circle-outline";
                            itemList.Add(ht);
                        }
                    }
                    list[i]["itemList"] = itemList;
                }
                return Helper.sendOKJson(Request, new { list = list });
            }
            catch
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误！");
            }
        }

        //通知 --数量
        public string getNotification()
        {
            try
            {
                DataTable dt_token = Helper.getToken(Request);
                int jiaoliuCount = Convert.ToInt32(DbHelper.ExecuteDataTable(
                                string.Format(@"select count(a.id) from t_jiaoliu a
                                                inner join t_jiazhang b on a.sendpersonid=b.jzid
                                                inner join t_student c on c.studentid=b.stuid
                                                inner join T_TeacherClassRelation d on d.classid=c.classid
                                                where a.readState=0 and d.teacherid='{0}'
                                                ", dt_token.Rows[0]["id"])).Rows[0][0]);
                return Helper.sendOKJson(Request, new { jiaoliuCount = jiaoliuCount });
            }
            catch {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误！");
            }
        }

        //模块 --添加
        public string modelsReport(int type)
        {
            try
            {
                DataTable dt_token = Helper.getToken(Request);
                DbHelper.ExecuteNonQuery(string.Format("insert into [T_ModelsReport](loginid,modelName,loginType,clickTime,id) values('{0}','{1}','{2}',getdate(),'{3}')", dt_token.Rows[0]["id"], type, "2", System.Guid.NewGuid().ToString()));
                return Helper.sendOKJson(Request, null);
            }
            catch
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误！");
            }
        }
        //模块 --添加
        public string modelsReport2(string userId,int modelNo)
        {
            try
            {
                DbHelper.ExecuteNonQuery(string.Format("insert into [T_ModelsReport](loginid,modelName,loginType,clickTime,id) values('{0}','{1}','{2}',getdate(),'{3}')", userId, modelNo, "2", System.Guid.NewGuid().ToString()));
                return Helper.sendOKJson(Request, null);
            }
            catch
            {
                return Helper.sendErrorJson(Request, "数据处理过程中发生错误！");
            }
        }
    }
}
